# -*- coding: utf-8 -*-
"""
Created on Thu Dec 16 01:31:53 2021

@author: 刘长奇
"""

import numpy as np
import pandas as pd
import matplotlib as plt

train_data=np.load("t10k-images.npy")#60000,784
train_label=np.load("t10k-labels.npy")#60000
test_data=np.load("train-images.npy")#10000,784
test_label=np.load("train-labels.npy")#10000
class_names = ['T-shirt','Trouser','Pullover','Dress','Coat','Sandal','Shirt','Sneaker','Bag','Ankle boot']

from sklearn.decomposition import PCA
pca = PCA(n_components=0.8)#降维到24维
proj = pca.fit_transform(train_data)
proj2 = pca.fit_transform(test_data)
#需要归一化处理数据
#节点个数要调整
#非自然中断能达到0.6左右的准确率
#灰度图的绘制
#imshow的安装
print(np.shape(proj),np.shape(proj2))

from sklearn.ensemble import RandomForestClassifier 
# 建立随机森林模型
rfc = RandomForestClassifier(n_estimators=30, random_state=0)
#第一个参数代表随机森林中树木的个数，往往是越多越好，但准确率会收敛，参数越大，计算时间越长
rfc = rfc.fit(train_data,train_label)       
#用训练集数据训练模型 

y_pred=[]
j=0
for i in range(np.shape(train_data)[0]):
    y_pred.append(rfc.predict([train_data[i]]))
for i in range(np.shape(train_data)[0]):
    if y_pred[i]==train_label[i]:
        j=j+1
print(j/60000)